<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" showFooter :title="getTitle" width="80%" :showOkBtn="false">

    <vxe-grid ref="tableRef" v-bind="gridOptions" :style="{height: (contentHeight*0.6)+'px'}">
      <template #toolbar_buttons>
        <vxe-button  status="primary" size="small" style="margin-left: 5px;" @click="testAgainOpenByLogFn" content="重新亮一次" :loadding="loadding"></vxe-button>
         <div>
           库位名称: {{kuweiName}}
         </div>
      </template>
    </vxe-grid>

    <vxe-grid ref="tableRef2" v-bind="gridOptions2" style="height:300px">
      <template #toolbar_buttons>
        发送记录:
      </template>
    </vxe-grid>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import {ref, computed, unref, reactive} from 'vue';
import { BasicForm, useForm, ApiSelect } from '@/components/Form';
import { BasicDrawer, useDrawerInner } from '@/components/Drawer';
import {itemColumns, itemColumns2} from './role.data';
import util from "@/utils/util";

import { useModal } from "@/components/Modal";
import { msLogDetail,testAgainOpenByLog,sendLogs } from '@/api/sys/iot';
import { useMessage } from "@/hooks/web/useMessage";
import { useUserStore } from "@/store/modules/user";
import {BasicTableProps} from "@/components/VxeTable";
const contentHeight = ref(window.innerHeight);

const userStore = useUserStore();

const [registerModal, { openModal }] = useModal();

const emit = defineEmits(['success', 'register']);
const isUpdate = ref(true);
const loadding = ref(true);
const kuweiName=ref("");

const dataList = ref({});
const { createMessage } = useMessage();

const id = ref("");

const gridOptions = reactive<BasicTableProps>({
  // keepSource: true,
  editConfig: { trigger: 'click', mode: 'cell', showStatus: true },
  border: true,
  showOverflow: true,
  size: 'mini',
  height: 'auto', // 'auto'
  columns: itemColumns,
  // filterConfig: {
  //   remote: true,
  // },
  expandConfig: {
    padding: true
  },
  exportConfig:{

  },
  showFooter: true,

  toolbarConfig: {
    slots: {
      buttons: 'toolbar_buttons',
    },
    refresh: true,
  },
  formConfig: {
    enabled: false,
  },
  pagerConfig: {
    enabled: false,

    // pageSize: 20,
  },
  proxyConfig: {
    enabled: false,
  },
});
const gridOptions2 = reactive<BasicTableProps>({
  // keepSource: true,
  editConfig: { trigger: 'click', mode: 'cell', showStatus: true },
  border: true,
  showOverflow: true,
  size: 'mini',
  height: 'auto', // 'auto'
  columns: itemColumns2,
  // filterConfig: {
  //   remote: true,
  // },
  expandConfig: {
    padding: true
  },
  exportConfig:{

  },
  showFooter: true,

  toolbarConfig: {
    slots: {
      buttons: 'toolbar_buttons',
    },
    refresh: true,
  },
  formConfig: {
    enabled: false,
  },
  pagerConfig: {
    enabled: false,

    // pageSize: 20,
  },
  proxyConfig: {
    enabled: false,
  },
});

const tableRef = ref();
const tableRef2 = ref();

const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  //打开弹层后的回调
  setDrawerProps({ confirmLoading: false });
  // 需要在setFieldsValue之前先填充treeData，否则Tree组件可能会报key not exist警告
  // if (unref(treeData).length === 0) {
  //   treeData.value = (await getMenuList()) as any as TreeItem[];
  // }
  isUpdate.value = !!data?.isUpdate;
  id.value = data.id;
  dataList.value=data;
  msLogDetailFn();
  sendLogsFn();
});



const getTitle = computed(() => (!unref(isUpdate) ? '明细' : '明细'));


function msLogDetailFn(){
  msLogDetail({id:id.value}).then((res) => {
    if(res.code==200){
      let map=new Map();
      let arr=[];
      res.data.forEach(item => {
        map.set(item.houseLocationName,item.houseLocationName)
      })
      map.forEach((value, key) => {
        // console.log(`${key}: ${value}`);
        arr.push(value);
      });
      kuweiName.value=arr.join(',');
      tableRef.value.loadData(res.data);
    }else{
      createMessage.error(res.message);
    }
  });
}

/**
 * 查看发送记录
 */
function sendLogsFn(){
  sendLogs({logId:id.value}).then((res) => {
    if(res.code==200){
     tableRef2.value.loadData(res.data);
    }else{
      createMessage.error(res.message);
    }
  });
}


function testAgainOpenByLogFn(){
  loadding.value = true;
  testAgainOpenByLog({id:id.value}).then((res) => {
    if(res.code==200){
      msLogDetailFn();
      createMessage.success(res.data);

    }else{
      createMessage.error(res.message);
    }
  }).finally(()=>{
    loadding.value = false;
  });
}

</script>
